package com.qf.wangqing.sportsnews.fragment.headline;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qf.wangqing.sportsnews.BR;
import com.qf.wangqing.sportsnews.MyApplication;
import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.adapter.headline.AdapterRecHead;
import com.qf.wangqing.sportsnews.bean.HeadEntity.HeadEntityTop;
import com.qf.wangqing.sportsnews.databinding.FragmentHeadheadBinding;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class HeadheadFragment extends Fragment implements Callback<HeadEntityTop> {

    private AdapterRecHead<Object> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FragmentHeadheadBinding dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_headhead, container, false);

        adapter = new AdapterRecHead<>(new ArrayList<Object>(), getContext(), R.layout.head_item, BR.data);

        dataBinding.headRecyclerView.setAdapter(adapter);


        MyApplication application = (MyApplication) getActivity().getApplication();
        application.getHeadNetService().getHead().enqueue(this);

        return dataBinding.getRoot();
    }

    @Override
    public void onResponse(Call<HeadEntityTop> call, Response<HeadEntityTop> response) {
        HeadEntityTop top = response.body();
        adapter.addAll(top.getData().getHeads());
    }

    @Override
    public void onFailure(Call<HeadEntityTop> call, Throwable t) {
        Toast.makeText(getContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
    }
}
